<?php

class edit_teammembers
{
	var $driverid;
	var $admin;
	var $approved;
	var $teamid;
	var $driverOptions;
	
	var $add_record;
	var $valid;

	function edit_teammembers($add_record, $teamid, $record='')
	{
		global $db;
		if($add_record)
		{
			$this->add_record = true;
			$this->driverid = 0;
			$this->approved = 0;
			$this->admin = 0;
			$this->teamid = $teamid;
			$this->valid = true;
			$driverOptions = '';
			$q_drivers = 'SELECT u.user_id, u.username, pf_drivername as user_drivername 
						  FROM ' . USERS_TABLE . ' u, '. USERSGROUPS_TABLE . ' g, ' . DRIVERNAME_TABLE . ' d 
				 		  WHERE u.user_id = d.user_id AND g.user_id = u.user_id AND g.group_id = 3 AND u.user_id NOT IN ' .
					'(SELECT tm_driver FROM ' . TEAMMEMBERS_TABLE . ' WHERE tm_team = ' . $this->teamid . ') ORDER BY pf_drivername ASC';	
			$result = $db->sql_query($q_drivers);
			while($row = $db->sql_fetchrow($result)) 
			{
				$driverOptions = $driverOptions . 
						'<option value="'.$row['user_id'].'" '. ($this->driverid==$row['user_id'] ? 'SELECTED' : '').'>'  . $row['user_drivername'] . 
											' (' . $row['username'] . ')</option>';
			}
			$this->driverOptions = $driverOptions;
		}
		else if(!$add_record)
		{
			$this->add_record = false;
			$this->driverid = $record['tm_driver'];
			$this->approved = $record['tm_approved'];
			$this->admin = $record['tm_admin'];
			$this->teamid = $teamid;
			$this->valid = true;
			$driverOptions = '';
			$q_drivers = 'SELECT u.user_id, u.username, pf_drivername as user_drivername 
						  FROM ' . USERS_TABLE . ' u, ' . DRIVERNAME_TABLE . ' d 
						  WHERE d.user_id = u.user_id AND u.user_id = ' . $this->driverid ;
			$result = $db->sql_query($q_drivers);
			while($row = $db->sql_fetchrow($result)) 
			{
				$driverOptions = $driverOptions . 
						'<option value="'.$row['user_id'].'" '. ($this->driverid==$row['user_id'] ? 'SELECTED' : '').'>'  . $row['user_drivername'] . 
											' (' . $row['username'] . ')</option>';
			}
			$this->driverOptions = $driverOptions;
		}
		else
		{
			$this->valid = false;
		}
	}
		
	function render()
	{		
		global $skin_dir, $lang;
		
		$submitText = $this->add_record ? $lang['AddButton'] : $lang['EditButton'];
		$submitValue = $this->add_record ? 'addRecord' : 'editRecord';
		$remS = $this->add_record ? '<!--' : '';
		$remE = $this->add_record ? '-->' : '';
		
		$admin_checked = $this->admin == 1 ? 'CHECKED' : '';
		$approved_checked = $this->approved == 1 ? 'CHECKED' : '';
		
		$teamid = $this->teamid;
								 
		$driverOptions = $this->driverOptions;
    	include $skin_dir . 'admin/editteammember.tpl';
	}
}
?>